﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.Interfaces;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 中止工单ViewModel
    /// </summary>
    public class AbortViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;
        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public AbortViewModel(ServiceProxy<IExecutionContract> iExecutionContract, IWindowManager windowManager)
        {
            this._iExecutionContract = iExecutionContract;
            this._windowManager = windowManager;
        }
        #endregion

        #region 属性

        #region 中止原因
        /// <summary>
        /// 中止原因
        /// </summary>
        [DependencyProperty]
        public string Reason { get; set; }
        #endregion

        #region 工单信息
        /// <summary>
        /// 工单信息
        /// </summary>
        [DependencyProperty]
        public WorkOrderInfo _workOrderInfo { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(AddViewModel addViewModel)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInfo workOrderInfo)
        {
            this._workOrderInfo = workOrderInfo;
        }
        #endregion


        #region 提交 —— async void Submit()

        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            #endregion

            this.Busy();
            await Task.Run(() => this._iExecutionContract.Channel.AbortWorkOrder(_workOrderInfo.Id, Reason));
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion
        #endregion
    }
}
